/**
 * 
 */
package com.clh.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.clh.common.PageInfo;
import com.clh.dao.FamilyDao;
import com.clh.dao.OwnerDao;
import com.clh.dao.impl.FamilyDaoImpl;
import com.clh.dao.impl.OwnerDaoImpl;
import com.clh.entity.Family;
import com.clh.entity.Owner;
import com.clh.service.OwnerService;
import com.clh.util.DBUtil;

/**
 * @Description: 业主service实现类
 * @author Administrator
 * @date 2022年5月26日 上午9:48:49
 * @version v1.0
 */

public class OwnerServiceImpl implements OwnerService {

	private OwnerDao ownerDao = new OwnerDaoImpl();



	@Override
	public PageInfo<Owner> getOwnerListByPage(Owner owner, int pageNum, int pageSize) {
		StringBuffer ownerName = new StringBuffer();
		// 判断参数情况，拼接字符串匹配符%
		if (owner.getOwnerName() != null && !"".equals(owner.getOwnerName())) {
			// 进行商品名关键字查询
			ownerName.append("%").append(owner.getOwnerName()).append("%");
			// 重新设值
			owner.setOwnerName(ownerName.toString());

		}
		// 1.调用持久层获取分页数据
		List<Owner> list = ownerDao.findByPage(owner, pageNum, pageSize);
		// 2.调用持久层获取分页之前的总记录数
		Integer total = ownerDao.countByCondition(owner);

		// 3.封装分页对象
		PageInfo<Owner> pageInfo = new PageInfo<Owner>(list, pageNum, pageSize, total);

		return pageInfo;

	}

	/**
	 * 删除实现类
	 */
	@Override
	public int deleteById(String roomNo) {
		return ownerDao.deleteById(roomNo);
	}

	@Override
	public Owner login(String phoneNumber, String password) {
		// 调用持久层
		System.out.println("service层"+phoneNumber+" 密码"+password);
		Owner owner=ownerDao.findByPhoneNumberAndPassword(phoneNumber, password);
		return owner;
	}

	
	/**
	 * 添加
	 */
	@Override
	public boolean addOwner(Owner owner) {
		
		boolean flag =  ownerDao.insert(owner) > 0;
		
		//添加业主成功后，往家庭成员信息表中加入业主信息 author：邱宏宇
		if(flag) {
			Family family = new Family();
			String familyName = owner.getOwnerName();
			String familyCard = owner.getOwnerId();
			String familyImage = owner.getOwnerImage();
			String roomNo = owner.getRoomNo();
			int familyTypeId = 5;
			String familyPhone = owner.getOwnerPhoneNumber();
			
			family.setFamilyName(familyName);
			family.setFamilyCard(familyCard);
			family.setFamilyImage(familyImage);
			family.setRoomNo(roomNo);
			family.setFamilyTypeId(familyTypeId);
			family.setFamilyPhone(familyPhone);
			//调用familyDao,暂不判断成功与否
			FamilyDao familyDaoImpl = new FamilyDaoImpl();
			familyDaoImpl.addFamily(family);
		}
		
		
		return flag;
		
		
	}

	
	
	/**
	 * 修改
	 */
	@Override
	public boolean modifyOwner(Owner owner) {
		
		return ownerDao.update(owner) > 0;
	}

	@Override
	public Owner getOwnerById(String roomNo) {
		
		return ownerDao.findById(roomNo);
	}

	@Override
	public List<Owner> getOwnerByPhone(String ownerPhoneNumber) {
		
		return ownerDao.findByPhone(ownerPhoneNumber);
	}

}
